package com.hmall;


import com.google.gson.Gson;
import com.hdmall.business.common.util.HttpClientUtil;
import com.hmall.vo.InsertVo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: victor
 * Date: 13-9-26
 * Time: 上午10:03
 */
public class RobotTest2
{
    public static void main(String[] args) throws AWTException,         InterruptedException, IOException
    {
        Robot robot = new Robot();
        //设置Robot产生一个动作后的休眠时间,否则执行过快
        robot.setAutoDelay(1000);

        //获取屏幕分辨率
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        System.out.println(d);
        //以屏幕的尺寸创建个矩形
        Rectangle screenRect = new Rectangle(d);
        //截图（截取整个屏幕图片）
        BufferedImage bufferedImage =       robot.createScreenCapture(screenRect);
        //保存截图
        File file = new File("screenRect.png");
        ImageIO.write(bufferedImage, "png", file);

        while (1==1){
            try {
                Map map = new HashMap();
                Integer id = null;
//                Long order_no = null;
//                Long group_no = null;
//                Integer user_id = null;
                String s = HttpClientUtil.doGetWithParam("http://47.98.169.251:9093/manage/order/getInsertOrder", map);
                System.out.println(s);
                Gson gson = new Gson();
                Map<String,Object> map2 = new HashMap<String, Object>();
                map = gson.fromJson(s, map.getClass());
                System.out.println(map);
                if (map.get("success").equals(false)){
                    System.out.println("无订单需插入,等待100s");
                    Thread.sleep(100000);
                }else {
                    System.out.println("开始插入订单");
                    JSONArray jsonArray = JSONArray.fromObject(map.get("data"));
                    System.out.println(jsonArray.size());
                    for (int i=0;i<jsonArray.size();i++){
                        InsertVo insertVo = (InsertVo) JSONObject.toBean((JSONObject) jsonArray.get(i),InsertVo.class);
                        System.out.println(insertVo.toString());
                        id = insertVo.getId();
//                        order_no = insertVo.getOrder_no();
//                        group_no = insertVo.getGroup_no();
//                        user_id = insertVo.getUser_id();
                        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                        if (i==0){
                            robot.mouseMove(920, 448);
                            clipboard.setContents(new StringSelection("wangwl"),null);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.delay(100);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_A);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_CONTROL);
                            Thread.sleep(2000);

                            robot.mouseMove(888, 493);
                            clipboard.setContents(new StringSelection("b2b123456"),null);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.delay(100);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_A);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_CONTROL);
                            Thread.sleep(2000);

                            robot.mouseMove(1184, 458);
                            System.out.println("单击");
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            Thread.sleep(4000);

                            robot.mouseMove(164, 239);
                            System.out.println("单击");
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            Thread.sleep(2000);

                            robot.mouseMove(458, 209);
                            System.out.println("双击");
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.delay(10);
                            robot.keyPress(KeyEvent.VK_ENTER);
                            Thread.sleep(2000);

                            //新增下拉
                            robot.mouseMove(149, 141);
                            System.out.println("单击");
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            Thread.sleep(2000);

                            //自制
                            robot.mouseMove(155, 175);
                            System.out.println("单击");
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            Thread.sleep(2000);

                            robot.mouseMove(281, 218);
                            clipboard.setContents(new StringSelection(insertVo.getSales_organization()),null);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_CONTROL);
                            Thread.sleep(2000);

                            robot.mouseMove(500, 248);
                            System.out.println("单击");
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            Thread.sleep(2000);

                            robot.mouseMove(267, 250);
                            clipboard.setContents(new StringSelection("ECC1-38-01"),null);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_CONTROL);
                            Thread.sleep(2000);

                            //填写客户
                            robot.mouseMove(1346, 252);
//                        clipboard.setContents(new StringSelection(insertVo.getBus_name()),null);
                            clipboard.setContents(new StringSelection(insertVo.getBus_name()),null);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_CONTROL);
                            Thread.sleep(2000);



                            //填写销售类型
                            robot.mouseMove(1051, 275);
                            clipboard.setContents(new StringSelection(insertVo.getSales_type()),null);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_CONTROL);
                            Thread.sleep(2000);

                            //填写运费支付方式
                            robot.mouseMove(1358, 274);
                            clipboard.setContents(new StringSelection(insertVo.getFreight_payment_type()),null);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_CONTROL);
                            Thread.sleep(2000);

                            //填写收款方式
                            robot.mouseMove(1607, 277);
                            clipboard.setContents(new StringSelection(insertVo.getPayment_methods()),null);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.delay(100);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_CONTROL);
                            Thread.sleep(2000);

//                    //填写合同号
//                    robot.mouseMove(821, 320);
//                    clipboard.setContents(new StringSelection("1111111"),null);
//                    robot.mousePress(InputEvent.BUTTON1_MASK);
//                    robot.mouseRelease(InputEvent.BUTTON1_MASK);
//                    robot.keyPress(KeyEvent.VK_CONTROL);
//                    robot.keyPress(KeyEvent.VK_V);
//                    robot.keyRelease(KeyEvent.VK_V);
//                    robot.keyRelease(KeyEvent.VK_CONTROL);
//                    Thread.sleep(2000);

                            //填写备注
                            robot.mouseMove(382, 327);
                            clipboard.setContents(new StringSelection(insertVo.getRemark()),null);
                            robot.mousePress(InputEvent.BUTTON1_MASK);
                            robot.mouseRelease(InputEvent.BUTTON1_MASK);
                            robot.keyPress(KeyEvent.VK_CONTROL);
                            robot.keyPress(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_V);
                            robot.keyRelease(KeyEvent.VK_CONTROL);
                            Thread.sleep(2000);
                        }

                        //填写物料编码
                        robot.mouseMove(311, 418+i*20);
                        clipboard.setContents(new StringSelection(insertVo.getMaterial_code()),null);
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        robot.keyPress(KeyEvent.VK_CONTROL);
                        robot.keyPress(KeyEvent.VK_V);
                        robot.keyRelease(KeyEvent.VK_V);
                        robot.keyRelease(KeyEvent.VK_CONTROL);
                        Thread.sleep(2000);

                        robot.mouseMove(562, 900);

                        robot.mousePress(InputEvent.BUTTON1_MASK);

                        robot.delay(100);//停顿100ms

                        robot.mouseMove(1017, 900);

                        robot.mouseRelease(InputEvent.BUTTON1_MASK);//释放左键

//                    //填写核定箱量
//                    robot.mouseMove(843, 408+i*20);
//                    clipboard.setContents(new StringSelection(insertVo.getQuantity().toString()),null);
//                    robot.mousePress(InputEvent.BUTTON1_MASK);
//                    robot.mouseRelease(InputEvent.BUTTON1_MASK);
//                    robot.keyPress(KeyEvent.VK_CONTROL);
//                    robot.keyPress(KeyEvent.VK_V);
//                    robot.keyRelease(KeyEvent.VK_V);
//                    robot.keyRelease(KeyEvent.VK_CONTROL);
//                    Thread.sleep(2000);

                        //填写核定重量
                        robot.mouseMove(843+93, 418+i*20);
                        clipboard.setContents(new StringSelection(insertVo.getQuantity().toString()),null);
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        robot.keyPress(KeyEvent.VK_CONTROL);
                        robot.keyPress(KeyEvent.VK_V);
                        robot.keyRelease(KeyEvent.VK_V);
                        robot.keyRelease(KeyEvent.VK_CONTROL);
                        Thread.sleep(2000);
                        //填写主含税单价
                        robot.mouseMove(1040, 418+i*20);
                        clipboard.setContents(new StringSelection(insertVo.getCurrentUnitPrice().toString()),null);
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        robot.keyPress(KeyEvent.VK_CONTROL);
                        robot.keyPress(KeyEvent.VK_V);
                        robot.keyRelease(KeyEvent.VK_V);
                        robot.keyRelease(KeyEvent.VK_CONTROL);
                        Thread.sleep(2000);
                        //填写出厂价
                        robot.mouseMove(1342, 418+i*20);
                        clipboard.setContents(new StringSelection(insertVo.getEx_factory_price().toString()),null);
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        robot.keyPress(KeyEvent.VK_CONTROL);
                        robot.keyPress(KeyEvent.VK_V);
                        robot.keyRelease(KeyEvent.VK_V);
                        robot.keyRelease(KeyEvent.VK_CONTROL);
                        Thread.sleep(2000);

                        robot.mouseMove(1017, 900);

                        robot.mousePress(InputEvent.BUTTON1_MASK);//按下左键

                        robot.mouseMove(1374, 900);

                        robot.mouseRelease(InputEvent.BUTTON1_MASK);//释放左键

                        //填写税率
                        robot.mouseMove(1431, 418+i*20);
                        clipboard.setContents(new StringSelection(insertVo.getRate()),null);
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        robot.keyPress(KeyEvent.VK_CONTROL);
                        robot.keyPress(KeyEvent.VK_V);
                        robot.keyRelease(KeyEvent.VK_V);
                        robot.keyRelease(KeyEvent.VK_CONTROL);
                        Thread.sleep(2000);

                        robot.mouseMove(1374, 900);

                        robot.mousePress(InputEvent.BUTTON1_MASK);//按下左键

                        robot.mouseMove(562, 900);

                        robot.mouseRelease(InputEvent.BUTTON1_MASK);//释放左键
                        Thread.sleep(2000);
                        //新增物料
                        robot.mouseMove(1374, 356);
                        System.out.println("单击");
                        robot.mousePress(InputEvent.BUTTON1_MASK);
                        robot.mouseRelease(InputEvent.BUTTON1_MASK);
                        Thread.sleep(2000);

                    }
                    //点击保存提交
                    robot.mouseMove(191, 148);
                    System.out.println("单击");
                    robot.mousePress(InputEvent.BUTTON1_MASK);
                    robot.mouseRelease(InputEvent.BUTTON1_MASK);
                    Thread.sleep(2000);

                    //点击注销上拉框
                    robot.mouseMove(1656, 996);
                    System.out.println("单击");
                    robot.mousePress(InputEvent.BUTTON1_MASK);
                    robot.mouseRelease(InputEvent.BUTTON1_MASK);
                    Thread.sleep(2000);

                    //点击注销
                    robot.mouseMove(1613, 947);
                    System.out.println("单击");
                    robot.mousePress(InputEvent.BUTTON1_MASK);
                    robot.mouseRelease(InputEvent.BUTTON1_MASK);
                    Thread.sleep(2000);

                    //点击确定注销
                    robot.mouseMove(1002, 582);
                    System.out.println("单击");
                    robot.mousePress(InputEvent.BUTTON1_MASK);
                    robot.mouseRelease(InputEvent.BUTTON1_MASK);
                    Thread.sleep(2000);
                    if (id!=null){
                        //新版启用下一段，此段弃用
                        Map map3 = new HashMap();
                        map3.put("id",id.toString());
                        String s2 = HttpClientUtil.doGetWithParam("http://47.98.169.251:9093/manage/order/changeInsertOrder", map3);
                        id = null;

//                        Map map4 = new HashMap();
//                        map4.put("orderNo",order_no.toString());
//                        map4.put("subOrderNo",group_no.toString());
//                        map4.put("userId",user_id.toString());
//                        String s1 = HttpClientUtil.doGetWithParam("http://47.98.169.251:9094/back/ncFinishPush", map4);
//                        order_no = null;
//                        group_no = null;
//                        user_id = null;
                    }
                    Thread.sleep(300000);
                }
            }catch (Exception e){
                System.out.println("连接超时");
            }
        }

    }
}
